<?php
class financeaction extends action {
    /**
     * test
     */
    public function test() {
        $db_inc = new IncomeModel();
        print_r($db_inc->get_inc_range());
    }

    /**
     * index
     */
    public function index() {
        echo '<li>welcome fms!</li>';
        echo '<li>'.date('y-m-d h:i').'</li>';
        echo '<li><a href="inc_add">inc_add</a></li>';
    }

    /**
     * income add
     */
    public function inc_add() {
        /*$res = array();
        
        $res['cat_id'] = 2;
        $res['inc_name'] = '';
        $res['amount'] = '';
        $res['create_time'] = time();
        
        $this->assign('do', 'add');
        $this->assign('res', $res);
		*/
       $this->display();
    }

    /**
     * income edit
     */
    public function inc_edit() {
        $inc_id = $_GET['inc_id'];

        $db_inc = new IncomeModel();
        $res = $db_inc->find($inc_id);

        $this->assign('do', 'edit');
        $this->assign('inc_id', $inc_id);
        $this->assign('res', $res);

        $this->display('inc_add');
    }

    /**
     * income delete
     */
    public function inc_delete() {
        $inc_id = $_GET['inc_id'];

        $db_inc = new IncomeModel();
        $data = $db_inc->find($inc_id);
        
        $db_inc->delete($inc_id);

		  log_write('delete '.json_encode($data));
		  
		  $this->assign('jumpUrl', 'inc_list');
        $this->success('删除成功!');
    }

    /**
     * income edit do
     */
    public function inc_edit_do() {
        $db_inc = new IncomeModel();
			
		  $data_o = $db_inc->find($_POST['inc_id']);

        $data = array();
        $data = $_POST;

        $_time = explode('-', $data['create_time']);
        $data['create_time'] = mktime(0, 0, 0, $_time[1], $_time[2], $_time[0]);

        $db_inc->save($data);

		  log_write('edit_do '.json_encode($data_o).' to '.json_encode($data));
        
        $this->assign('jumpUrl', 'inc_list');
        $this->success('修改成功!');
    }

    /**
     * income add do
     */
    public function inc_add_do() {
        $db_inc = new IncomeModel();
        
        //数据
        $data = array();
        $data = $_POST;

        $_time = explode('-', $data['create_time']);
        $data['create_time'] = mktime(0, 0, 0, $_time[1], $_time[2], $_time[0]);

        //存入
        $db_inc->add($data);
        
        log_write('add_do '.json_encode($data));

        $this->assign('jumpUrl', 'inc_list');
        $this->success('新增成功!'); 
          
       
    }

    /**
     * income list
     */
    public function inc_list() {
        $db_inc = new IncomeModel();
        $inc_range = $db_inc->get_inc_range();
        
        $res = $db_inc->findAll();

        $sum = 0;
        //整理
        $res_by_time = array();
        for($i=$inc_range['time_min']; $i<=$inc_range['time_max']; $i+=86400) {
            $res_by_time[$i]['info']['sum'] = 0;
            $res_by_time[$i]['info']['time'] = $i;
        }

        foreach($res as $row) {
            $res_by_time[$row['create_time']]['data'][] = $row;
            
            $res_by_time[$row['create_time']]['info']['sum'] += $row['amount'];
            $sum += $row['amount'];
        }
        //echo ('<pre>');
        //print_r($res_by_time);

        $this->assign('sum', $sum);
        $this->assign('day_count', ($inc_range['time_max']-$inc_range['time_min'])/86400+1);
        $this->assign('inc_range', $inc_range);
        $this->assign('res', $res_by_time);
        
        $this->display();
    }

    /**
     * payout add
     */
    public function pay_add() {
    
    }

    /**
     * category add
     */
    public function cat_add() {
    
    }

}

?>
